using CustomControls;
using System.Data;

namespace 自定义控件
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //FunnelControl funnel = new FunnelControl();
            //funnel.Location = new Point(50, 50);
            //funnel.Size = new Size(200, 300);
            //funnel.WaterLevel = 60; // 设置水位
            //funnel.FlowRate = 3.0f; // 设置水流速度
            //this.Controls.Add(funnel);


            // 创建开关按钮
            //ToggleButton toggle = new ToggleButton();
            //toggle.Location = new Point(50, 50);
            //toggle.Size = new Size(60, 30);
            //toggle.IsOn = true; // 默认开启
            //toggle.CheckedChanged += Toggle_CheckedChanged;
            //this.Controls.Add(toggle);


            // 创建控件
            //DualListBoxControl dualListBox = new DualListBoxControl();
            //dualListBox.Location = new Point(10, 10);
            //dualListBox.Size = new Size(500, 300);
            //this.Controls.Add(dualListBox);

            //// 添加初始项目
            //dualListBox.SourceListBox.Items.AddRange(new[] { "项目1", "项目2", "项目3" });

            //// 处理项目移动事件
            //dualListBox.ItemMoved += DualListBox_ItemMoved;


            // 创建控件
            DataGridControl dataGrid = new DataGridControl();
            dataGrid.Dock = DockStyle.Fill;
            //dataGrid.Location = new Point(10, 10);
            //dataGrid.Size = new Size(800, 400);
            this.Controls.Add(dataGrid);

            // 设置数据源
            DataTable dt = GetDataFromDatabase(); // 从数据库获取数据
            dataGrid.SetDataSource(dt);

            // 处理添加事件
            dataGrid.AddRequested += DataGrid_AddRequested;

            // 处理编辑事件
            dataGrid.EditRequested += DataGrid_EditRequested;

            // 处理删除事件
            dataGrid.DeleteRequested += DataGrid_DeleteRequested;

            //// 使用CenterPanel控件
            //CenterPanel centerPanel = new CenterPanel();
            //centerPanel.Dock = DockStyle.Bottom; // 填充整个窗体
            //centerPanel.BackColor = Color.LightGray; // 设置背景色

            //// 添加一些示例控件到CenterPanel
            //for (int i = 0; i < 10; i++)
            //{
            //    Button btn = new Button();
            //    btn.Text = $"按钮 {i + 1}";
            //    btn.Size = new Size(150, 120);
            //    btn.Margin = new Padding(5);
            //    centerPanel.Controls.Add(btn);
            //}

            //this.Controls.Add(centerPanel);

        }

        private DataTable GetDataFromDatabase()
        {
            //throw new NotImplementedException();
            // 创建数据表
            DataTable dataTable = new DataTable("PersonData");

            // 定义列结构
            dataTable.Columns.Add("name", typeof(string));    // 姓名字段
            dataTable.Columns.Add("age", typeof(int));       // 年龄字段
            dataTable.Columns.Add("phone", typeof(string));  // 电话字段

            // 添加示例数据行
            dataTable.Rows.Add("张三", 28, "13812345678");
            dataTable.Rows.Add("李四", 32, "13987654321");
            dataTable.Rows.Add("王五", 45, "13755556666");

            return dataTable;
        }

        private void DataGrid_AddRequested(object sender, EventArgs e)
        {
            //// 显示添加数据的对话框
            //AddDataDialog dialog = new AddDataDialog();
            //if (dialog.ShowDialog() == DialogResult.OK)
            //{
            //    // 添加数据到数据库
            //    SaveDataToDatabase(dialog.Data);
            //    // 刷新表格
            //    dataGrid.RefreshData();
            //}
        }

        private void DataGrid_EditRequested(object sender, DataRowEventArgs e)
        {
            //// 显示编辑数据的对话框
            //EditDataDialog dialog = new EditDataDialog(e.Row);
            //if (dialog.ShowDialog() == DialogResult.OK)
            //{
            //    // 更新数据库
            //    UpdateDataInDatabase(dialog.Data);
            //    // 刷新表格
            //    dataGrid.RefreshData();
            //}
        }

        private void DataGrid_DeleteRequested(object sender, DataRowEventArgs e)
        {
            // 从数据库删除数据
            //DeleteDataFromDatabase(e.Row);
            //// 刷新表格
            //dataGrid.RefreshData();
        }


        private void DualListBox_ItemMoved(object sender, DualListBoxControl.ItemMovedEventArgs e)
        {
            MessageBox.Show($"移动了 {e.Items.Count} 个项目，方向：" +
                           (e.IsFromSource ? "源→目标" : "目标→源"));
        }

        // 事件处理
        private void Toggle_CheckedChanged(object sender, EventArgs e)
        {
            ToggleButton tb = (ToggleButton)sender;
            MessageBox.Show($"开关状态: {tb.IsOn}");
        }
    }
}
